package {{ packageName }}.sample.infrastructure.secondary;

import {{ packageName }}.IntegrationTest;
import com.datastax.oss.driver.api.core.CqlSession;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.cassandra.core.cql.CqlTemplate;
import org.springframework.data.cassandra.core.cql.session.DefaultSessionFactory;

import static {{ packageName }}.sample.domain.BeersIdentityFixture.cloackOfFeathersId;
import static {{ packageName }}.sample.domain.beer.BeersFixture.beer;
import static org.assertj.core.api.Assertions.assertThat;

@IntegrationTest
class SpringDataRepositoryIT {

  private static final String FIND_BEER = "select writetime(name) from beer where id = ?";
  private static final String FIND_CATALOG = "select writetime(name) from beer_catalog where id = ?";

  @Autowired
  private SpringDataBeersRepository repository;

  @Autowired
  private CqlSession cqlSession;

  @Test
  void shouldSaveInBatch() {
    repository.save(beer());

    CqlTemplate cqlTemplate = new CqlTemplate();
    cqlTemplate.setSessionFactory(new DefaultSessionFactory(cqlSession));

    long beerInsertionTime = cqlTemplate.queryForObject(FIND_BEER, Long.class, cloackOfFeathersId().get());
    long catalogInsertionTime = cqlTemplate.queryForObject(FIND_CATALOG, Long.class, cloackOfFeathersId().get());

    assertThat(beerInsertionTime).isEqualTo(catalogInsertionTime);
  }
}
